#ifndef DATABASE_H_
#define DATABASE_H_

#include "scHeader.h"
#include "RequestDescriptor.h"
#include "ColDesc.h"
#include <QString>

namespace sybilpm
{
/**
 * SQLte frontend
 */
class Database
{
public:
	Database(const QString dbFile);
	virtual ~Database();
	QString errorString() const;
	bool selectPragma(QXmlStreamWriter * writer, int * versMaj, int * versMin, int * versBld);
	/**
	 * Get meta information during boot. The method does not access DataTable
	 * for attribute information, thus it can be used before these objects
	 * are created.
	 */
	void selectBoot(const Request & request, QXmlStreamWriter * writer);
	/**
	 * Select according to Request. The output is written either to an XML
	 * stream (if writer!=0) or as temporary DataObjects (if writer==0).
	 * If depRequests==0 follow-FK in the request is ignored.
	 */
	void select(const Request & request, QXmlStreamWriter * writer, RequestDescriptor * depRequests);
	void update(QXmlStreamWriter * writer, const Request & request);

private:
	void handleError(const char * sqliteCall, sqlite3_stmt *statement = NULL, bool useExeption = true);
	QString sqlPkList(const QString & key, const Pks & pks) const;
	QString sqltimeConstr(TimeType deadline) const;
	sqlite3 *db;
	QString dbFile;
	int sqRc;
	bool initOk;
};

} // namespace sybilpm

#endif /* DATABASE_H_ */
